package com.base.cn.platform.os.controller.qa.question;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.entity.manage.config.SysConfig;
import com.base.cn.platform.os.entity.qa.question.QaQuestion;
import com.base.cn.platform.os.service.course.qa.qatype.QaTypeService;
import com.base.cn.platform.os.service.course.qa.question.QaQuestionService;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.manage.teacher.SysTeacherService;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LIZHIPENG
 * @since 2018/4/16
 */
@Controller
@RequestMapping("/admin/qa/question")
public class AdminQuestionController extends AdminBaseController {

    @Autowired
    private QaQuestionService questionService;
    @Autowired
    private QaTypeService qaTypeService;
    @Autowired
    private SysTeacherService sysTeacherService;
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 查询免费问题列表
     * @param model
     * @return
     */
    @RequestMapping("/queryFreeQuestionList")
    public String queryFreeQuestionList(Model model){
        Map<String, Object> params = new HashMap<>();
        List<Map<String, Object>> qaTypeList = qaTypeService.queryQaTypeList(params);
        params.put("isFree", 1);
        model.addAttribute("qaTypeList", qaTypeList);
        model.addAttribute("isFree", 1);
        return resultPage("/qa/question/qa-question-list");
    }

    /**
     * 查询付费问题列表
     * @param model
     * @return
     */
    @RequestMapping("/queryNoFreeQuestionList")
    public String queryNoFreeQuestionList(Model model){
        Map<String, Object> params = new HashMap<>();
        List<Map<String, Object>> qaTypeList = qaTypeService.queryQaTypeList(params);
        params.put("isFree", 2);
        model.addAttribute("qaTypeList", qaTypeList);
        model.addAttribute("isFree", 2);
        return resultPage("/qa/question/qa-question-list");
    }

    /**
     * ajax加载问题列表
     * @param model
     * @param currentPage 当前页码
     * @param params 查询条件
     * @return
     */
    @RequestMapping("/ajaxQueryQuestionList")
    public String ajaxQueryQuestionList(HttpServletRequest request,
                                        Model model,
                                        @RequestParam(value = "currentPage", required = false,defaultValue = "1")int currentPage,
                                        @RequestBody Map<String, Object> params){
        if(params.get("isFree").toString().equals("2")){//如果是收费问答
            JsonObject sysUser = loginUtil.getLoginUser(request);
            BigDecimal sysUserId = sysUser.get("id").getAsBigDecimal();
            if(sysUserId.compareTo(new BigDecimal(1)) !=0){
                BigDecimal teacherId = sysUser.get("teacherId").getAsBigDecimal();
                if(teacherId.compareTo(new BigDecimal(0)) !=0){
                    params.put("teacherId",teacherId);
                }
            }
        }
        model.addAttribute("isFree", params.get("isFree"));
        if(ObjectUtils.isNotEmpty(params.get("isFree"))&&params.get("isFree").equals("1")){
            params.put("notNoFree",true);
            params.remove("isFree");
        }
        params.put("selectFrom",1);//区分前后台查询，1是后台查询列表，2是前台查询列表
        Map<String, Object> dataMap = questionService.queryQuestionListPage(currentPage, 10, params, null,true, true, true, false, false, false, false,true);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/qa/question/ajax-qa-question-list");
    }

    /**
     * 查询问题详情
     * @param model
     * @param questionId
     * @return
     */
    @RequestMapping(value={"/freeQuestionInfo","/noFreeQuestionInfo"})
    public String questionInfo(Model model,HttpServletRequest request,
                               @RequestParam("questionId") BigDecimal questionId){
        BigDecimal userId = loginUtil.getLoginUserId(request);
        QaQuestion question = questionService.findQuestionById(questionId, true, true, true, true, false, false, false);
        int isFree =question.getIsFree();
        if(isFree == 2){
            Map<String, Object> teacher = question.getTeacherMap();
            Integer questionTeacherSysUserId = (Integer) teacher.get("sysUserId");
            if(userId.intValue() == questionTeacherSysUserId || userId.intValue() == 1){
                model.addAttribute("isTeacherOfQuestion", true);
            }else {
                model.addAttribute("isTeacherOfQuestion", false);
            }
        }else {
            model.addAttribute("isTeacherOfQuestion", true);
        }
        model.addAttribute("question", question);
        return resultPage("/qa/question/qa-question-info");
    }

    /**
     * 修改问题状态
     * @param questionIds 问题ID串
     * @param status 状态：1.恢复，2.禁用，3.删除
     * @return
     */
    @RequestMapping("/updateQuestionStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "修改问题状态")
    public Map<String, Object> updateQuestionStatus(@RequestParam("questionIds")String questionIds,
                                                    @RequestParam("status")int status){
        return questionService.updateQuestionStatus(questionIds, status);
    }

    /**
     * 后台保存问题回答方法
     * @param request
     * @param question
     * @return
     */
    @RequestMapping("/saveQuestionReply")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "保存问题回答")
    public Map<String, Object> saveQuestionReply(HttpServletRequest request,
                                                 @RequestBody Map<String, Object> question){
        BigDecimal sysUserId = loginUtil.getLoginUserId(request);
        question.put("replyUserId", sysUserId);
        return questionService.saveQuestionReply(question);
    }

    /**
     * 获取问答配置信息
     *
     * @param model
     * @return
     */
    @RequestMapping("/queryQaConfig")
    public String queryQaConfig(Model model){
        SysConfig attent = sysConfigService.getSysConfigByConfigType("ATTENT");
        model.addAttribute("sysConfig",attent);
        return resultPage("/qa/question/qa-config");
    }

}
